local Player = { }
--[[
Player = {
	game_clt_id = CGameCltId
	account = ""
	log = log:new("player." .. account)

	hp = 0,  -- 血量，为0时判为失败

    x = 0.0,    --玩家的坐标
    y = 0.0,
    z = 0.0,
}
--]]

local Log = require("log")
local pb = require("protobuf")

-- 不依赖 user_mgr 获取 account, 而是由调用者传入 account.
-- 因为将来多服实现时， cell 服上没有 user_mgr。
function Player:new(game_clt_id, account)
    assert(game_clt_id)
    assert(account)
    local INIT_HP = 100
    local player = {
        game_clt_id = game_clt_id,
        account = account,
        log = Log:new("tanks.player_" .. account),
        hp = INIT_HP,
        x = 0.0,
        y = 0.0,
        z = 0.0,
    }
    player.log:debug("New player. account=%s", account)

    setmetatable(player, self)
    self.__index = self
    return player
end  -- new()

function Player:is_valid_player()
    return true
end

function Player:rpc_request(service_name, method_name, request_str, callback)
    assert("string" == type(service_name))
    assert("string" == type(method_name))
    assert("string" == type(request_str))
    assert(not callback or "function" == type(callback))
    self.log:debug("rpc request: %s.%s", service_name, method_name)
    c_rpc.request_by_base(self.game_clt_id, service_name, method_name,
    request_str, callback)
end  -- rpc_request()

function Player:enter_room(peer_name, is_peer_tank1)
    local req = { peer_name = peer_name, is_peer_tank1 = is_peer_tank1 }
    local req_str = pb.encode("tanks.EnterRoomMsg", req)
    self:rpc_request("tanks.TanksPush", "EnterRoom", req_str)
end  -- enter_room()

-- 转发移动消息
function Player:forward_move_msg(position_msg)
    self.x = position_msg.x
    self.y = position_msg.y
    self.z = position_msg.z
    local req_str = pb.encode("tanks.PositionMsg", position_msg)
    self:rpc_request("tanks.TanksPush", "PeerMoveTo", req_str)
end  -- forward_move_msg()

-- 转发旋转消息
function Player:rotate_msg(rotation_msg)
    local req_str = pb.encode("tanks.RotationMsg", rotation_msg);
    self:rpc_request("tanks.TanksPush", "PeerRotateTo", req_str)
end  -- forward_move_msg()

-- 转发开火消息
function Player:fire_msg(fire_msg)
    local req_str = pb.encode("tanks.FireMsg", fire_msg);
    self:rpc_request("tanks.TanksPush", "PeerFire", req_str)
end  -- forward_move_msg()

-- 转发伤害消息
function Player:explode_msg(damage_msg)
    self.log:info("player:explode_msg")

    local req_str = pb.encode("tanks.DamageMsg", damage_msg);
    self:rpc_request("tanks.TanksPush", "Damage", req_str)
end

function Player:send_frame_msg(msg_list)
    if msg_list == nil then
        self.log:info("this msg_list is nil")
        return
    end

    local msgs = {}
    for k,v in pairs(msg_list) do
        table.insert(msgs,v)
    end

    local resp = { FrameMsgListContent = msgs }
    local req_str = pb.encode("tanks.FrameMsgList", resp)
    self:rpc_request("tanks.TanksPush", "PushFrameMsgList", req_str)
end

return Player
